package com.gwtgeo.ui.client.mvp.desktop;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.user.client.ui.Widget;
import com.gwtgeo.ui.client.mvp.desktop.DesktopPresenter.MenuItemDescriptor;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.menu.Menu;
import com.smartgwt.client.widgets.menu.MenuButton;
import com.smartgwt.client.widgets.menu.MenuItem;

public class DesktopView extends VLayout implements DesktopPresenter.Display {

	private HLayout menuBar;
	private VLayout workArea;
	private Map<String, MenuItem> menuItems;
	private Map<String, MenuButton> menuButtons;

	public DesktopView() {
		super();
		this.menuItems = new HashMap<String, MenuItem>();
		this.menuButtons = new HashMap<String, MenuButton>();
		this.setWidth100();
		this.setHeight100();
		this.buildMenuBar();
		this.buildWorkArea();
	}

	private void buildMenuBar() {

		this.menuBar = new HLayout();
		this.menuBar.setMembersMargin(5);
		this.menuBar.setWidth100();
		this.menuBar.setHeight(28);
		this.menuBar.setAlign(Alignment.LEFT);
		this.menuBar.setBackgroundImage("/images/menu_bar.gif");

		this.addMember(this.menuBar);
	}

	private void buildWorkArea() {
		this.workArea = new VLayout();
		this.workArea.setBackgroundColor("LightSteelBlue");
		this.workArea.setWidth100();
		this.workArea.setHeight100();
		this.addMember(this.workArea);
	}

	@Override
	public void addMenu(String name, List<MenuItemDescriptor> descriptors) {

		Menu menu = new Menu();
		menu = new Menu();
		menu.setShowShadow(true);
		menu.setShadowDepth(10);

		for (MenuItemDescriptor descriptor : descriptors) {
			MenuItem item = new MenuItem(descriptor.getLabel());
			this.menuItems.put(descriptor.getId(), item);
			descriptor.setMenuItem(item);
			menu.addItem(item);
		}

		MenuButton menuButton = new MenuButton(name, menu);
		menuButton.setLayoutAlign(VerticalAlignment.CENTER);
		this.menuButtons.put(name, menuButton);
		this.menuBar.addMember(menuButton);
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public Canvas getWorkArea() {
		return workArea;
	}
	

	@Override
	public void disableMenuItems(List<String> ids) {
		for (String id : ids) {
			MenuItem item = this.menuItems.get(id);
			if (item != null) {
				item.setEnabled(false);
			}
		}
	}

	@Override
	public void enableMenuItems(List<String> ids) {
		for (String id : ids) {
			MenuItem item = this.menuItems.get(id);
			if (item != null) {
				item.setEnabled(true);
			}
		}
	}

	@Override
	public void disableMenuButton(List<String> names) {
		for (String name : names) {
			MenuButton menuButton = menuButtons.get(name);
			if (menuButton != null) {
				menuButton.disable();
			}
		}
	}

	@Override
	public void enableMenuButton(List<String> names) {
		for (String name : names) {
			MenuButton menuButton = menuButtons.get(name);
			if (menuButton != null) {
				menuButton.enable();
			}
		}
	}

}
